﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace ControllerClient
{
    public class ComputerHelper
    {
        public static Dictionary<string,string> GetComputerInfo()
        {
            var info = new Dictionary<string,string>();
            string cpu = GetCPUInfo();
            string baseBoard = GetBaseBoardInfo();
            string bios = GetBIOSInfo();
            string mac = GetMACInfo();
            Console.WriteLine("==mac" + mac);
            //info.Add("cpu", cpu);
            //info.Add("baseBoard", baseBoard);
            //info.Add("bios", bios);
            info.Add("mac", mac);
            return info;
        }
        private static string GetCPUInfo()
        {
            string info = string.Empty;
            info = GetHardWareInfo("Win32_Processor", "ProcessorId");
            return info;
        }
        private static string GetBIOSInfo()
        {
            string info = string.Empty;
            info = GetHardWareInfo("Win32_BIOS", "SerialNumber");
            return info;
        }
        private static string GetBaseBoardInfo()
        {
            string info = string.Empty;
            info = GetHardWareInfo("Win32_BaseBoard", "SerialNumber");
            return info;
        }
        private static string GetMACInfo()
        {
            string info = string.Empty;
            //info = GetMacAddress();//GetHardWareInfo("Win32_NetworkAdapterConfiguration", "MACAddress");
            List<string> list = GetMacAddress();
            foreach(string item in list)
            {
                info = info + item + ";";
            }
            Console.WriteLine("info" + info);
            return info;
        }

        private static List<string> GetMacAddress()
        {
            List<string> list = new List<string>();
            var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject)o;
                if (!(bool)mo["IPEnabled"]) continue;
                string mac = mo["MacAddress"].ToString();
                list.Add(mac);
            }


            //NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

            //foreach (var networkInterface in interfaces)
            //{
            //    if (networkInterface.OperationalStatus == OperationalStatus.Up && !networkInterface.Description.Contains("Loopback"))
            //    {
            //        PhysicalAddress physicalAddress = networkInterface.GetPhysicalAddress();
            //        mac = physicalAddress.ToString();
            //        Console.WriteLine($"MAC Address: {physicalAddress}");
            //    }
            //}
            return list;
        }

        private static string GetHardWareInfo(string typePath, string key)
        {
            try
            {
                ManagementClass managementClass = new ManagementClass(typePath);
                ManagementObjectCollection mn = managementClass.GetInstances();
                PropertyDataCollection properties = managementClass.Properties;
                foreach (PropertyData property in properties)
                {
                    if (property.Name == key)
                    {
                        foreach (ManagementObject m in mn)
                        {
                            return m.Properties[property.Name].Value.ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //这里写异常的处理
            }
            return string.Empty;
        }
    }
}
